<template>
  <div>
    <!-- 整个页面需要显示内容的区域 -->
    <div class="container">
      <!-- 顶部区域 -->
      <div class="header">
        <h1>学茶商城运营管理平台</h1>
      </div>
      <!-- 登录表单 -->
      <div class="login-form">
        <el-form ref="loginForm" :model="loginParam" :rules="rules" label-width="0px" class="demo-ruleForm">
          <el-form-item label="" prop="username">
            <el-input v-model="loginParam.username">
              <template slot="prepend"><i class="el-icon-user-solid"/></template>
            </el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input v-model="loginParam.password">
              <template slot="prepend"><i class="el-icon-s-ticket"/></template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%;"
                       @click="submitForm(loginForm)">登录
            </el-button>
          </el-form-item>
        </el-form>
        <div style="text-align: center; font-size: 12px; color: #666;">
          测试前台用户模拟支付，请 <a href="/alipay/login">点击这里</a> 。
        </div>
      </div>
      <!-- 版权信息 -->
      <div class="copyright">
        <p>Copyright &copy; 达内时代科技集团 - IT学院 - Java教学研发部</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ElMessage, ElMessageBox} from "element-plus";
import BASE_URL from "@/http/BaseUrl";
import SimpleAxios from "@/http/SimpleAxios";


const loginForm = ref()
const loginParam = ref({
  username: 'root',
  password: '123456'
})
const rules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 4, max: 15, message: '用户名的长度必须是 4 到 15 个字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 4, max: 15, message: '密码的长度必须是 4 到 15 个字符', trigger: 'blur'}
  ]
}
const submitForm = (form) => {
  form.validate((valid) => {
    if (valid) {
      let url = BASE_URL.GATEWAY + '/passport/user/login';
      SimpleAxios.post(url, loginParam.value).then((response) => {
        if (response.data.state == 200) {
          ElMessage.success('登录成功！')
          let userInfo = response.data.data;
          localStorage.setItem('userInfo', JSON.stringify(userInfo));
          router.push('/');
        } else {
          ElMessageBox.alert('错误', response.data.message, {
                confirmButtonText: '确认',
                // callback:
              }
          )
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        ElMessageBox.alert(message, title, {
          confirmButtonText: '确定',
          callback: (action) => {
            ElMessage({
              type: 'info',
              message: `action: ${action}`,
            })
          },
        });
        console.log(error);
      });
    } else {
      return false;
    }
  });
}

</script>

<style>
body {
  height: 100vh;
  background-image: linear-gradient(to bottom left, #5abae8, #3c4bd2);
  background-repeat: no-repeat;
}

a {
  color: #3c4bd2;
  text-decoration: none;
}

a:hover {
  color: #212a79;
  text-decoration: underline;
}

.container {
  width: 460px;
  margin: 0 auto;
}

.header {
  margin-top: 50px;
}

.header h1 {
  text-align: center;
  color: #fff;
}

.login-form {
  background: #fff;
  border-radius: 10px;
  padding: 50px 50px 20px 50px;
}

.copyright {
  margin-top: 20px;
}

.copyright p {
  text-align: center;
  font-size: 11px;
  color: #fff;
}
</style>